package com.ddwl.common.controller;

import cn.hutool.core.util.StrUtil;
import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.exception.IllegalException;
import com.ddwl.common.exception.ParamException;
import com.ddwl.common.feign.api.ShopApi;
import com.ddwl.schema.bo.base.BaseBo;
import com.ddwl.schema.vo.base.RestVo;
import com.ddwl.schema.vo.shop.ShopVo;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * @author Fu Zhaohui
 * @version 1.0.0
 * @date 2019/6/14 7:38
 */
public abstract class BaseController {


    @Resource
    private ShopApi shopApi;

    protected Map<String, String> getAuthMap() {
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes) ra;
        HttpServletRequest request = sra.getRequest();
        return Optional.ofNullable((Map < String, String >) request.getAttribute("authMap")).orElse(new HashMap<>(0));
    }

    /**
     * 获取用户ID
     *
     * @return
     */
    protected String getUserId() {
        return getAuthMap().get("uid");
    }


    protected String getUserNameNew() {
        Map<String,String> map=getAuthMap();
        if(map==null){
            return "";
        }else {
        return getAuthMap().get("nickname");
        }
    }

    /**
     * 获取商户ID
     *
     * @return
     */
    protected String getMchId() {
        return getAuthMap().get("mch_id");
    }

    /**
     * 获取产品
     *
     * @return
     */
    protected String getProduct() {
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes) ra;
        HttpServletRequest request = sra.getRequest();
        return request.getHeader("pk");
    }

    /**
     * 获取店铺ID
     * @return
     */
    protected String getShopId() {
        Map<String, String> authMap = getAuthMap();
        String shopId = authMap.get("shopId");
        ParamException.isTrue(StrUtil.isBlank(shopId),"当前用户店铺数据异常");
        return shopId;
    }

    /**
     * 获取店铺ID
     * @return
     */
    protected String getShopName() {
        Map<String, String> authMap = getAuthMap();
        String shopId = authMap.get("shopName");
        ParamException.isTrue(StrUtil.isBlank(shopId),"当前用户店铺数据异常");
        return shopId;
    }



    /**
     * 获取平台
     *
     * @return
     */
    protected String getPlatform() {
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes) ra;
        HttpServletRequest request = sra.getRequest();
        return request.getHeader("pt");
    }

    /**
     * 获取产品
     *
     * @return
     */
    protected String getDk() {
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes) ra;
        HttpServletRequest request = sra.getRequest();
        return request.getHeader("dk");
    }

    protected void fillBaseField(BaseBo bo) {
        Map<String, String> authMap = getAuthMap();
        bo.setOperator(authMap.get("uid"));
        bo.setOperatorName(authMap.get("realname"));
        if(StrUtil.isBlank(bo.getOperatorName())){
            bo.setOperatorName(authMap.get("nickname"));
        }
        bo.setProduct(authMap.get("pk"));
        bo.setDk(authMap.get("dk"));
        bo.setShopId(authMap.get("shopId"));
    }

    public ShopVo getShopVo(String shopId) {
        ShopVo shopVo;
        if (StrUtil.isEmpty(shopId)) {
            shopVo = shopApi.getSimpleShopByUserId(getUserId()).getResult();
        }else{
            shopVo = shopApi.judgeUserHasShop(getUserId(), shopId).getResult();
        }
        IllegalException.isNull(shopVo,"您无权操作该店铺数据");
        return shopVo;
    }
}
